//
//  AEHomePageSubTopView.m
//  AESOLAR_IOS
//
//  Created by Bin Tang on 2024/10/18.
//  Copyright © 2024 Mr.Wang. All rights reserved.
//

#import "AEHomePageSubTopView.h"
#import "AE_Solar-Swift.h"
#import "AEHomePageSubViewModel.h"

@interface AEHomePageSubTopView () <UITextFieldDelegate>

@property (nonatomic, strong) AEHomePageSubViewModel *viewModel;

@property (nonatomic, strong) UIView *bgView;

@property (nonatomic, strong) UIImageView *searhImgView;

@property (nonatomic, strong) UIButton *btnSearch;

/// 搜索输入框
@property (nonatomic, strong) UITextField *searchTextField;

@end

@implementation AEHomePageSubTopView

- (void)initWithSubView {
    self.backgroundColor = [UIColor whiteColor];
    
    [self initWithBgView];
    
    [self initWithSearhImgView];
    
    [self initWithBtnSearch];
}

- (void)initWithBgView {
    if (!_bgView) {
        CGRect frame = CGRectMake(15, (self.height - 32) * 0.5, self.width - 15 * 2, 32);
        _bgView = [[UIView alloc] initWithFrame:frame];
        _bgView.backgroundColor = [UIColor colorWithHexString:@"#F5F5F5"];
        _bgView.layer.cornerRadius = 6;
        _bgView.layer.masksToBounds = YES;
        [self addSubview:_bgView];
    }
}

- (void)initWithSearhImgView {
    if (!_searhImgView) {
        UIImage *image = [UIImage imageNamed:@"ic_main_search"];
        CGRect frame = CGRectMake(15, (self.bgView.height - image.size.height) * 0.5, image.size.width, image.size.height);
        _searhImgView = [UIImageView createWithImageView:self.bgView frame:frame defaultImage:image propertiesBlock:^(UIImageView * _Nonnull imageView) {
            
        }];
    }
}

- (void)initWithBtnSearch {
    if (!_btnSearch) {
        NSString *title = [AEStringExten localizedValueWithKey:@"搜索"];
        UIFont *font = [UIFont systemFontOfSize:14 weight:UIFontWeightBold];
        float width = [title widthForFont:font] + 2 + 12 * 2;
        CGRect frame = CGRectMake(self.bgView.width - width - 4, (self.bgView.height - 32) * 0.5, width, 32);
        _btnSearch = [UIButton createWithButton:self.bgView frame:frame font:font target:self action:@selector(actionWithSearch:) propertiesBlock:^(UIButton * _Nonnull button) {
            [button setTitle:title forState:UIControlStateNormal];
            [button setTitleColor:kWhiteColor forState:UIControlStateNormal];
            button.backgroundColor = kThemeColor;
            button.layer.cornerRadius = 6;
            button.layer.masksToBounds = YES;
        }];
    }
}

- (void)initWithSearchTextField {
    if (!_searchTextField) {
        CGRect frame = CGRectMake(self.searhImgView.right + 5, 0, self.btnSearch.left - self.searhImgView.right - 5 - 5, 32);
        _searchTextField = [[UITextField alloc] initWithFrame:frame];
        _searchTextField.placeholder = [AEStringExten localizedValueWithKey:@"请输入活动、新闻等内容"];
        _searchTextField.font = [UIFont systemFontOfSize:13 weight:UIFontWeightRegular];
    //    _searchTextField.backgroundColor = [UIColor redColor];//[UIColor colorWithHexString:@"#F2F2F2"];
        _searchTextField.delegate = self;
        _searchTextField.borderStyle = UITextBorderStyleNone;
        _searchTextField.returnKeyType = UIReturnKeySearch;
        _searchTextField.keyboardType = UIKeyboardTypeDefault;
        _searchTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
        [self.bgView addSubview:_searchTextField];
        
        RAC(self.viewModel, searchMsg) = self.searchTextField.rac_textSignal;
    }
}

/// 搜索
/// - Parameter sender: sender description
- (void)actionWithSearch:(id)sender {
    [self.searchTextField resignFirstResponder];
        
    if (self.touchSearchBlock) {
        self.touchSearchBlock();
    }
}




- (void)updateWithViewData:(id)data {
    self.viewModel = data;
    
    [self initWithSearchTextField];
}

#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    [self actionWithSearch:nil];
    return true;
}

@end
